const express = require('express')
const { insertOrder, updateOrderState } = require('../mysql/order')
const Result = require('../result/result')
const { NO_PERMISSION } = require('../result/reponseState')
const router = express.Router()

router.post('/add', (req, res) => {
  const { order } = req.body
  const orderArr = []
  const date = new Date()
  order.forEach(e => {
    const i = [e.order_num, e.goodsInfo_id, e.goods_count, e.user_id, date, e.address_id]
    orderArr.push(i)
  });
  insertOrder(orderArr).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})
router.put('/cancel', (req, res) => {
  const { orderNum } = req.query
  updateOrderState(orderNum, 30).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

module.exports = router